﻿using System.Data.Entity;
using System.Data.SqlClient;

namespace Uinside.CreditSystem.DomainModels.Models
{
    public class CreditContext : DbContext
    {
        public CreditContext()
            : base("Data Source=.;Initial Catalog=CreditSystem;Integrated Security=True")
        {
            //this.Configuration.LazyLoadingEnabled = false;
            //this.Configuration.ProxyCreationEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BankAccount>().HasRequired(c => c.Bank).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Payment>().HasRequired(c => c.Borrower).WithMany(e => e.BorrowerPayments).WillCascadeOnDelete(false);
            modelBuilder.Entity<Payment>().HasRequired(c => c.Lender).WithMany(e => e.LenderPayments).WillCascadeOnDelete(false);
            modelBuilder.Entity<Payment>().HasRequired(c => c.Status).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Payment>().HasRequired(c => c.Type).WithMany().WillCascadeOnDelete(false);

            modelBuilder.Entity<Payment>().HasOptional(c => c.Order).WithMany(e=>e.Payments).WillCascadeOnDelete(false);
            modelBuilder.Entity<Order>().HasRequired(c => c.Borrower).WithMany(e => e.BorrowerOrder).WillCascadeOnDelete(false);
            modelBuilder.Entity<Order>().HasRequired(c => c.Lender).WithMany(e => e.LenderOrders).WillCascadeOnDelete(false);

            base.OnModelCreating(modelBuilder);
        }

        public DbSet<Customer> Customers { get; set; }
        public DbSet<BankAccount> BankAccounts { get; set; }
        public DbSet<Image> Images { get; set; }
        public DbSet<Payment> Payments { get; set; }
        public DbSet<Setting> Settings { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Order> Orders { get; set; }
    }
}
